/**
*  \file  BPSK_DemodStructCreate.c
*  \brief BPSK??????????????.
*/

#include"../../head/mBPSK_Demodulator/BPSK_Demodulator.h"
#include "../../head/LoadINIFile.h"//??????? ???????

double B_bps_arr[2][4462]={{0},{0}};


//double B_3bps[4462];
//double B_4bps[3347];
//double B_5bps[2678];
//double B_6bps[2232];
//double B_7bps[1913];
//double B_8bps[1674];
//double B_9bps[1488];
//double B_10bps[1340];
//double B_25bps[537];
//double B_6p25bps[2143];
//double B_12p5bps[1072];


/**
*  \brief BPSK??????????????.
*
*  \param [in]   Fc_Hz                   ??????.
*  \param [in]   Rb_bps                  ?????????.
*  \param [in]   Fs_Hz                   ??????.
*  \param [out] ModuleStruct             BPSK????????.
*  \return                               Empty
*
*/

void  BPSK_DemodStructCreate(FILE* INIFileStreamIn, double Fc_Hz, double Rb_bps, double Fs_Hz, PtrBPSKDemodStruct ModuleStruct,int hzType)
{
    //double getDoubleValTmp;							// ????????????????????int????????
    //double* getDoubleValTmpPtr = &getDoubleValTmp;	// ????????????????????????????????
    //unsigned int getDoubleValTmpLen; //= sizeof(getDoubleValTmp) / sizeof(double);				// ?????????
    //double defaultValueTmp;							// ????????????

    unsigned int j;
    ModuleStruct->LPF = (PtrLPFilter)malloc(sizeof(VarLPFilter));//????????DemodulatorDelete????
    if (Rb_bps == 3)
    {
        ModuleStruct->LPF->LPFLENGTH = 4462;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);//????????DemodulatorDelete????

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_3bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH, B_bps_arr[hzType]);	//??????

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);//????????DemodulatorDelete????
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);//????????DemodulatorDelete????
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }

    }
    else if (Rb_bps == 4)
    {

        ModuleStruct->LPF->LPFLENGTH = 3347;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_4bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }

    }
    else if (Rb_bps == 5)
    {

        ModuleStruct->LPF->LPFLENGTH = 2678;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_5bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }

    }
    else if (Rb_bps == 6)
    {
        ModuleStruct->LPF->LPFLENGTH = 2232;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_6bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }


    }else if (Rb_bps == 6.25 )
    {
        ModuleStruct->LPF->LPFLENGTH = 2143;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_6p25bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }


    }
    else if (Rb_bps == 7)
        {
            ModuleStruct->LPF->LPFLENGTH = 1913;
            ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

            GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_7bps",
                &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

            ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
            {
                ModuleStruct->LPF->ZI[j] = 0;
                ModuleStruct->LPF->ZQ[j] = 0;
            }

        }
        else if (Rb_bps == 8)
        {
            ModuleStruct->LPF->LPFLENGTH = 1488;
            ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

            GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_8bps",
                &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

            ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
            {
                ModuleStruct->LPF->ZI[j] = 0;
                ModuleStruct->LPF->ZQ[j] = 0;
            }

        }
        else if (Rb_bps == 9)
        {
            ModuleStruct->LPF->LPFLENGTH = 1340;
            ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

            GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_9bps",
                &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

            ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
            {
                ModuleStruct->LPF->ZI[j] = 0;
                ModuleStruct->LPF->ZQ[j] = 0;
            }
        }
        else if (Rb_bps == 10)
        {
            ModuleStruct->LPF->LPFLENGTH = 2566;
            ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

            GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_10bps",
                &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

            ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
            {
                ModuleStruct->LPF->ZI[j] = 0;
                ModuleStruct->LPF->ZQ[j] = 0;
            }
        }
    else if (Rb_bps == 12.5)
    {
        ModuleStruct->LPF->LPFLENGTH = 1072;
        ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

        GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_12p50bps",
            &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

        ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
        for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
        {
            ModuleStruct->LPF->ZI[j] = 0;
            ModuleStruct->LPF->ZQ[j] = 0;
        }
    }
    else if (Rb_bps == 25)
        {
            ModuleStruct->LPF->LPFLENGTH = 537;
            ModuleStruct->LPF->b = NEW(ModuleStruct->LPF->LPFLENGTH, double);

            GetKeyValue(INIFileStreamIn, "BPSK_Demodulator", "B_25bps",
                &ModuleStruct->LPF->b, &ModuleStruct->LPF->LPFLENGTH,  B_bps_arr[hzType]);	//??????

            ModuleStruct->LPF->ZI = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            ModuleStruct->LPF->ZQ = NEW((ModuleStruct->LPF->LPFLENGTH) - 1, double);
            for (j = 0; j < (ModuleStruct->LPF->LPFLENGTH) - 1; j++)
            {
                ModuleStruct->LPF->ZI[j] = 0;
                ModuleStruct->LPF->ZQ[j] = 0;
            }
        }

    ModuleStruct->Fc_Hz = Fc_Hz;
    ModuleStruct->Rb_bps = Rb_bps;
    ModuleStruct->Fs_Hz = Fs_Hz;
    ModuleStruct->CarrierPhase = 0;

}
